package com.hexiang.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hexiang.domain.CommonResponse;
import com.hexiang.domain.User;
import com.hexiang.service.UserHttpService;
import com.hexiang.utils.OkHttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class UserHttpServiceImpl implements UserHttpService {

    private static final String USER_HTTP_URL = "http://localhost:8080";

    @Resource
    private ObjectMapper objectMapper;

    @Override
    public CommonResponse getUsers() {
        CommonResponse commonResponse;
        String url = USER_HTTP_URL + "/users";
        try {
            commonResponse = CommonResponse.builder().body(OkHttpUtil.get(url, Collections.emptyMap())).succ(true).build();
        } catch (Exception e) {
            log.error("getUsersErr", e);
            commonResponse = CommonResponse.builder().code(500).msg("调用服务报错").succ(false).build();
        }

        return commonResponse;
    }

    @Override
    public CommonResponse getUserByName(String name) {
        CommonResponse commonResponse;

        String url = USER_HTTP_URL + "/user/" + name;
        try {
            commonResponse = CommonResponse.builder().body(OkHttpUtil.get(url, Collections.emptyMap())).succ(true).build();
        } catch (Exception e) {
            log.error("getUserByNameErr", e, name);
            commonResponse = CommonResponse.builder().code(500).msg("调用服务报错").succ(false).build();
        }

        return commonResponse;
    }

    @Override
    public CommonResponse addUser(String name, int age) {
        CommonResponse commonResponse;
        String url = USER_HTTP_URL + "/user";
        try {

            // 提交参数设置
            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("name", name);
            paramMap.put("age", String.valueOf(age));
            commonResponse = CommonResponse.builder().body(OkHttpUtil.post(url, paramMap)).succ(true).build();

        } catch (Exception e) {
            log.error("addUserErr", e, String.format("name is %s,age is %s", name, age));
            commonResponse = CommonResponse.builder().code(500).msg("调用服务报错").succ(false).build();
        }
        return commonResponse;
    }

    @Override
    public CommonResponse deleteUserById(Long id) {
        CommonResponse commonResponse;
        String url = USER_HTTP_URL + "/user/" + id;
        try {
            OkHttpUtil.delete(url);
            commonResponse = CommonResponse.builder().succ(true).build();
        } catch (Exception e) {
            log.error("deleteUserByIdErr", e, id);
            commonResponse = CommonResponse.builder().code(500).msg("调用服务报错").succ(false).build();
        }
        return commonResponse;
    }

    @Override
    public CommonResponse<Serializable> updateUser(User user) {

        CommonResponse<Serializable> commonResponse;
        String url = USER_HTTP_URL + "/user/";
        try {
            Map<String, String> paramMap = null;
            try {
                paramMap = objectMapper.readValue(objectMapper.writeValueAsString(user), Map.class);
            } catch (JsonProcessingException e) {
                log.error("格式错误", e, user);
                commonResponse = CommonResponse.builder().code(500).msg("格式错误").succ(false).build();
                return commonResponse;
            }
            if (!CollectionUtils.isEmpty(paramMap)) {
                OkHttpUtil.post(url, paramMap);
            }
            commonResponse = CommonResponse.builder().succ(true).build();
        } catch (Exception e) {
            log.error("updateUserErr", e, user);
            commonResponse = CommonResponse.builder().code(500).msg("调用服务报错").succ(false).build();
        }
        return commonResponse;
    }

    @Override
    public CommonResponse<Serializable> getUserPage(Integer pageNum, Integer pageSize) {

        CommonResponse<Serializable> commonResponse;
        String url = USER_HTTP_URL + "/user/page";
        try {
            Map<String, String> paramMap = new HashMap<>(16);
            paramMap.put("pageNum", String.valueOf(pageNum));
            paramMap.put("pageSize", String.valueOf(pageSize));
            commonResponse = CommonResponse.builder().body(OkHttpUtil.get(url, paramMap)).succ(true).build();

        } catch (Exception e) {
            log.error("getUserPageErr", e, String.format("pageNum is %s,pageSize is %s", pageNum, pageSize));
            commonResponse = CommonResponse.builder().code(500).msg("调用服务报错").succ(false).build();
        }
        return commonResponse;
    }
}